import { lazy } from "react";
import { RouterItem, RouterList, DataItem } from "../utils/type";
import {
    DesktopOutlined,
    FileOutlined,
    PieChartOutlined,
    TeamOutlined,
    UserOutlined,
} from '@ant-design/icons';
//读取本地存储的routes

let routes = localStorage.getItem('routes') ? JSON.parse(localStorage.getItem('routes') as string) : []
console.log(routes)
const renderRoutes = (routes: DataItem[]) => {
    let arr: RouterItem[] = [];
    routes && routes.forEach((item, index) => {
        //如果孩子存在则
        if(item.children){
             arr.push(
            {
                path: item.path,
                element:  lazy(() => import('../pages/'+item.element+'/index')),
                title: item.title,
                icon: item.icon,
                children:renderRoutes(item.children)
            }
        )
        //否则
        }else{
             arr.push(
            {
                path: item.path,
                element:  lazy(() => import('../pages/'+item.element+'/index')),
                title: item.title,
                icon: item.icon
            }
        )
        }
       
    })

    return  arr;


}

 export const homeRoutes:RouterItem[] =renderRoutes(routes)
// const stystemRoutes:RouterItem[]=[
//     {
//         path:'/home/stystem/user',
//         element:lazy(()=>import('../pages/stystem/user')),
//         title:'用户管理',
//         icon:DesktopOutlined
//     },
//      {
//         path:'/home/stystem/menu',
//         element:lazy(()=>import('../pages/stystem/menu')),
//         title:'菜单管理',
//         icon:FileOutlined
//     }
// ]

// export const homeRoutes:RouterItem[] = [
//     {
//         path: '/home/index',
//         element: lazy(() => import('../pages/index/index')),
//         title: '首页',
//         icon: PieChartOutlined,
//     },
//     {
//         path: '/home/role',
//         element: lazy(() => import('../pages/role/index')),
//         title: '角色管理',
//         icon: TeamOutlined,
//     },
//     {
//         path: '/home/stystem',
//         element: lazy(() => import('../pages/stystem/index')),
//         title: '系统管理',
//         icon: UserOutlined,
//         children: stystemRoutes
//     }
// ]
const routesList: RouterList = {
    mode: 'history',
    routes: [
        {
            path: '/',
            to: '/home/index',
        },
        {
            path: '/home',
            element: lazy(() => import('../pages/home')),
            children: homeRoutes
        },
        {
            path: '/login',
            element: lazy(() => import('../pages/login')),
        },
        {
            path: '*',
            element: lazy(() => import('../pages/error')),
        }
    ]
}

export default routesList